let Patient_List=new Vue({
    el:"#allpatients",
    data:{
        patients:[],
        pageInfo:"",
        pageSize:5,
        patient:""
    },
    methods:{
        loadAllPatients:function(pageNum) {
            let pageSize=$("#select1").val()
            console.log("当前的pageSize:"+pageSize)
            if(!pageNum){
                pageNum=1;
            }
            axios({
                url:"/portal/patient/allpatients",
                method:"get",
                params:{
                    pageNum:pageNum,
                    pageSize:pageSize
                }
            }). then(function(r) {
                if(r.status===200){
                    Patient_List.patients=r.data.list;
                    Patient_List.pageInfo=r.data;
                    console.log(JSON.stringify(r.data))
                }

            })
        },
        loadTHePatient:function (p){
            edit_patient.loadTHePatient(p);
        },
        //删除病人
        deleteThePatient:function (id){
            axios({
                url:"/portal/patient/deletepatient",
                method:"get",
                params:{
                    id:id
                }
            }).then(function (r) {
                alert(r.data);
                    location.reload();
            })
        }

    },
    created:function() {
        this.loadAllPatients();
    }

})


let edit_patient=new Vue({
    el:"#mymodal",
    data:{
        patient:"",
        officeId:1,
        departments:[],
        doctors:[],
        doctorId:0,
        file:""
    },
    methods : {
        loadTHePatient : function(p) {
            edit_patient.patient=p
            //加载部门
            axios({
                url : "/add_appointment/department",
                method:"get",
            }).then(function(r) {
                edit_patient.departments=r.data;
                console.log(edit_patient.departments)
            })

        },
        //异步请求加载选中科室的医生
        loadDoctors:function() {
            console.log("接收到了要加载的科室id:"+edit_patient.officeId);
            //判断选中的不是"请选择---"那个选项
            if(edit_patient.officeId==="ABC"){
                edit_patient.doctors=[];
                return;
            }
            //发出异步请求,上传科室的id
            axios({
                url:"/portal/doctors/getdoctorsbyofficeid",
                method:'get',
                params:{
                    id:edit_patient.officeId
                }
            }).then(function(r) {
                edit_patient.doctors=r.data;
            })

        },
        //获取并显示上传的图片
        getFile:function (event) {
            //获取发生事件input元素的第一个文件
            edit_patient.file=event.target.files[0];
            //创建一个fileReader对象
            var fileReader=new FileReader();
            //读取图片文件数据为DataUrl
            fileReader.readAsDataURL(edit_patient.file)
            //读取文件
            fileReader.onload=function(e) {
                //将这个url绑定到img元素的src属性
                $("#img").attr("src",e.target.result)
            }
        },
        //上传添加的病人信息
        submitpatient:function(p) {
           console.log("修改后的patient信息:"+JSON.stringify(p))
            //获取性别选项的结果
            edit_patient.gender=$(":radio:checked").val();
            //生成form表单
            let form=new FormData;
            form.append("id",p.id)
            form.append("name",p.name)
            form.append("idCard",p.idCard)
            form.append("doctorId",p.doctorId)
            form.append("departmentId",edit_patient.officeId)
            console.log("edit_patient.officeId:"+edit_patient.officeId)
            form.append("phone",p.phone)
            form.append("headImg",edit_patient.file)
            console.log("edit_patient.officeId:"+edit_patient.file)
            form.append("history",p.history)
            form.append("gender",p.gender)
            //异步发送post请求上传信息
            axios({
                url:"/portal/patient/updatepatient",
                method:"post",
                data:form
            }).then(function (r) {
               alert(r.data);
               if(r.data==="更新成功"){
                   location.reload();
               }
            })

        },

    }
})
